let fs = require("fs");

const COPY_DIR = __dirname + "\\copy"; //复制后的文件夹


const startRecovery = () => {

    //删除文件夹
    let exist = fs.existsSync(COPY_DIR);

    if (exist) {
        fs.rmdirSync(COPY_DIR, {});
    }

    fs.mkdirSync(COPY_DIR);

    let buffer = fs.readFileSync("./temp.bin");
    let offset = 0;
    let totalLen = buffer.readUint32BE(offset); //读取总长
    offset = offset + 32; //移动offset
    console.log(totalLen);
    let fileNum = buffer.readUint32BE(offset); //读取文件个数
    offset = offset + 32; //移动offset
    console.log(fileNum)

    for (let i = 0; i < fileNum; i++) {

        let nameLen = buffer.readUint32BE(offset);
        offset = offset + 32; //移动offset
        let bodyLen = buffer.readUint32BE(offset);
        offset = offset + 32; //移动offset

        let nameBuffer = buffer.subarray(offset, offset + nameLen);
        let name = nameBuffer.toString();
        console.log(name)
        offset = offset + nameLen;
        let bodyBuffer = buffer.subarray(offset, offset + bodyLen);
        offset = offset + bodyLen;
        let dirDelimiterIndex = name.lastIndexOf("\\");
        if (dirDelimiterIndex !== 0) {
            let dirName = name.slice(0, dirDelimiterIndex);
            fs.mkdirSync(COPY_DIR + dirName);
        }
        fs.writeFileSync(COPY_DIR + name, bodyBuffer);
    }


}

startRecovery();
